//
//  ExchangeThreeViewController.m
//  LineMoney
//
//  Created by 范琦 on 16/7/14.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ExchangeThreeViewController.h"
#import "ExchangeTwoTopView.h"
#import "MainSectionView.h"
#import "ExchangeTwoCell.h"
#import "ExchangeThreeCell.h"
#import "ExchangeFourView.h"
#import "InputTextField.h"
#import "ExchangeAddViewController.h"
#import "ExchangeSuccessViewController.h"

@interface ExchangeThreeViewController ()<UITableViewDataSource, UITableViewDelegate, KeyInputTextField, UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) ExchangeTwoTopView *topView;

@property (nonatomic, strong) MainSectionView *sectionView;

@property (nonatomic, strong) NSMutableArray *arr;

@property (nonatomic, strong) NSMutableArray *arr1;

@property (nonatomic, strong) NSMutableArray *textArr;

@property (nonatomic, strong) NSMutableArray *textFieldArr;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) ExchangeFourView *fourView;

@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIPickerView *pickView2;

@property (nonatomic, strong) UITextField *textFiled; // 当前选中的text

@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) NSMutableArray *yearArr;
@property (nonatomic, strong) NSMutableArray *dayArr;
@property (nonatomic, strong) NSMutableArray *monthArr;

@property (nonatomic, strong) UIView *pickTopView;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) NSString *datStr;
@property (nonatomic, strong) NSString *monthStr;

@end

@implementation ExchangeThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.listArr = [NSMutableArray arrayWithObjects:@"CD",@"CD",@"CD", nil];
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    [self.view addSubview:_mainView];
    _mainView.backgroundColor = HUIBACKGROUNDCOLOR;
    _str = @"范琦";
    [self creatRightBtn];
    [self creatPickerView];
    self.title = @"收款账户";
    [self creatScroll];
    [self creatTopView];
    [self creatSectionView];
    [self creatTableView];
    [self creatBottomButton];
    [self creatFourView];
    
    
}
- (void)creatRightBtn
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30, 30);
//    button.backgroundColor = [UIColor redColor];
    UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithCustomView:button];
    [button setImage:[UIImage imageNamed:@"添加-0"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = btn;
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ExchangeAddViewController *add = [[ExchangeAddViewController alloc]init];
        add.str1 = @"收款";
        [self.navigationController pushViewController:add animated:YES];
    }];
    

}
- (void)creatScroll
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 - 50)];
    _scrollView.backgroundColor = HUIBACKGROUNDCOLOR;
    _scrollView.contentSize = CGSizeMake(0, 590);
    [_mainView addSubview:_scrollView];
    _scrollView.showsVerticalScrollIndicator = NO;


}

- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeTwoTopView" owner:nil options:nil] lastObject];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 71);
    [self.scrollView addSubview:_topView];
    _topView.ImageV.image = [UIImage imageNamed:@"进程33"];
    
}

- (void)creatSectionView
{
    self.sectionView = [[[NSBundle mainBundle] loadNibNamed:@"MainSectionView" owner:nil options:nil] lastObject];
    [_scrollView addSubview:_sectionView];
    _sectionView.frame = CGRectMake(0, 81, KSCREENWIDTH, 40);
    _sectionView.backgroundColor = [UIColor whiteColor];
    _sectionView.leftView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"小标题前竖条"]];
    _sectionView.label.textColor = BACKGROUNDCOLOR;
    _sectionView.backgroundColor = [UIColor whiteColor];
    _sectionView.label.text = @"现有记录";
    [[_sectionView.btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.view endEditing:YES];
        [self creatPickerView:_sectionView.btn];
    }];
    
}
- (void)creatBottomButton
{
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    nextBtn.backgroundColor = BACKGROUNDCOLOR;
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [_mainView addSubview:nextBtn];
    nextBtn.frame = CGRectMake(0, KSCREENHEIGHT - 64 - 40 , KSCREENWIDTH, 40);
    nextBtn.tintColor = [UIColor whiteColor];
    [[nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_mainView addSubview:_backView];
        [_mainView addSubview:_fourView];

    }];
}
- (void)creatFourView
{
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.5;
    self.fourView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeFourView" owner:nil options:nil] lastObject];
    
    _fourView.frame = CGRectMake(15, 130 - 64, KSCREENWIDTH - 30, 419);
    _textFieldArr = [NSMutableArray arrayWithObjects:_fourView.textField1,_fourView.textField2, _fourView.textField3, _fourView.textField4, _fourView.textField5, _fourView.textField6, nil];
    _fourView.textField1.KeyInputDelegate = self;
    _fourView.textField2.KeyInputDelegate = self;
    _fourView.textField3.KeyInputDelegate = self;
    _fourView.textField4.KeyInputDelegate = self;
    _fourView.textField5.KeyInputDelegate = self;
    _fourView.textField6.KeyInputDelegate = self;
    _fourView.textField1.tag = 10000;
    _fourView.textField2.tag = 20000;
    _fourView.textField3.tag = 30000;
    _fourView.textField4.tag = 40000;
    _fourView.textField5.tag = 50000;
    _fourView.textField6.tag = 60000;
    [[_fourView.enterBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ExchangeSuccessViewController *success = [[ExchangeSuccessViewController alloc]init];
        [self.navigationController pushViewController:success animated:YES];
    }];
    

#warning 密码逻辑处理 未简化
    [[_fourView.textField1 rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
        UITextField *text = x;
        if (![self judgeIsNumber:text.text] && text.text.length != 0) {
            _fourView.textField1.text = @"";
        }else{
        if (_fourView.textField1.text.length == 1) {
            [self changeTextField:_fourView.textField2];
            [_fourView.textField2 becomeFirstResponder];
            }
        }
        
    }];
    
    [[_fourView.textField2 rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
        UITextField *text = x;
        if (![self judgeIsNumber:text.text] && text.text.length != 0) {
            _fourView.textField2.text = @"";
        }else{
            if (_fourView.textField2.text.length == 1) {
                [self changeTextField:_fourView.textField3];
                [_fourView.textField3 becomeFirstResponder];
            }else if (_fourView.textField2.text.length == 0){
                [self changeTextField:_fourView.textField1];
                [_fourView.textField1 becomeFirstResponder];
            }
        }
    }];
    
    [[_fourView.textField3 rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
        UITextField *text = x;
        if (![self judgeIsNumber:text.text] && text.text.length != 0) {
            _fourView.textField3.text = @"";
        }else{
            if (_fourView.textField3.text.length == 1) {
                [self changeTextField:_fourView.textField4];
                [_fourView.textField4 becomeFirstResponder];
            }else if (_fourView.textField3.text.length == 0){
                [self changeTextField:_fourView.textField2];
                [_fourView.textField2 becomeFirstResponder];
            }
        }

    }];
    
    [[_fourView.textField4 rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
        UITextField *text = x;
        if (![self judgeIsNumber:text.text] && text.text.length != 0) {
            _fourView.textField4.text = @"";
        }else{
            if (_fourView.textField4.text.length == 1) {
                [self changeTextField:_fourView.textField5];
                [_fourView.textField5 becomeFirstResponder];
            }else if (_fourView.textField4.text.length == 0){
                [self changeTextField:_fourView.textField3];
                [_fourView.textField3 becomeFirstResponder];
            }
        }
    }];
    
    [[_fourView.textField5 rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
        UITextField *text = x;
        if (![self judgeIsNumber:text.text] && text.text.length != 0) {
            _fourView.textField5.text = @"";
        }else{
            if (_fourView.textField5.text.length == 1) {
                [self changeTextField:_fourView.textField6];
                [_fourView.textField6 becomeFirstResponder];
            }else if (_fourView.textField5.text.length == 0){
                [self changeTextField:_fourView.textField4];
                [_fourView.textField4 becomeFirstResponder];
            }
        }
    }];
    
    [[_fourView.textField6 rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
        UITextField *text = x;
        if (![self judgeIsNumber:text.text] && text.text.length != 0) {
            _fourView.textField6.text = @"";
        }else{
            if (_fourView.textField6.text.length == 1) {
                [_fourView.textField6 resignFirstResponder];

            }else if (_fourView.textField6.text.length == 0){
                [self changeTextField:_fourView.textField5];
                [_fourView.textField5 becomeFirstResponder];
            }
        }
    }];
    
    [[_fourView.backBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_backView removeFromSuperview];
        [_fourView removeFromSuperview];
        
        
    }];
    

    
}
#warning 控制只允许当前输入框输入
- (void)changeTextField:(UITextField *)text
{
    for (UITextField *text1 in _textFieldArr) {
        if (text != text1) {
            text1.enabled = NO;
        }else{
            text1.enabled = YES;
        }
    }

}

- (void)deleteBackward:(UITextField *)text
{
    if (text.text.length == 0) {
        switch (text.tag) {
            case 10000:
                
                break;
            case 20000:
                _fourView.textField1.text = @"";
                [self changeTextField:_fourView.textField1];
                [_fourView.textField1 becomeFirstResponder];
                break;
            case 30000:
                _fourView.textField2.text = @"";
                [self changeTextField:_fourView.textField2];
                [_fourView.textField2 becomeFirstResponder];
                break;
            case 40000:
                _fourView.textField3.text = @"";
                [self changeTextField:_fourView.textField3];
                [_fourView.textField3 becomeFirstResponder];
                break;
            case 50000:
                _fourView.textField4.text = @"";
                [self changeTextField:_fourView.textField4];
                [_fourView.textField4 becomeFirstResponder];
                break;
            case 60000:
                _fourView.textField5.text = @"";
                [self changeTextField:_fourView.textField5];
                [_fourView.textField5 becomeFirstResponder];
                break;
            default:
                break;
        }
        
        
    }
}
- (BOOL)judgeIsNumber:(NSString *)str
{
    NSString *regex = @"[0-9]";
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if ([predicate evaluateWithObject:str] == YES) {
        return YES;
    }
    return NO;
}

- (void)creatTableView
{
    self.arr = [NSMutableArray arrayWithObjects:@"收款人姓名",@"电话",@"电子邮件", nil];
    self.arr1 = [NSMutableArray arrayWithObjects:@"选择国家",@"城市",@"省",@"州",@"邮编", nil];

    self.textArr = [NSMutableArray arrayWithObjects:@"王姜博",@"13589854452",@"21064445544@qq.com", nil];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120, KSCREENWIDTH, 55 * 9 + (10 * 7)) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.scrollView addSubview:_tableView];
    _tableView.scrollEnabled = NO;
    _tableView.rowHeight = 55;
    [_tableView registerNib:[UINib nibWithNibName:@"ExchangeTwoCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"ExchangeThreeCell" bundle:nil] forCellReuseIdentifier:@"cell1"];

    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.sectionFooterHeight = 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 1;
    }
    
    return 5;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        ExchangeThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [[cell.leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [cell selectLeftBtn];
        }];
        
        [[cell.rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [cell selecRightBtn];
        }];
              
        return cell;
    }else{
    
        ExchangeTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (indexPath.section == 0) {
            cell.label.text = _arr[indexPath.row];
            cell.text.text = _textArr[indexPath.row];
            cell.imageV.hidden = YES;
 
        }else if (indexPath.section == 2)
        {
            if (indexPath.row == 4) {
                cell.imageV.hidden = YES;
            }
            cell.text.inputView = _pickView;
            [[cell.text rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
                self.textFiled = x;
            }];
            cell.label.text = _arr1[indexPath.row];
            

        }
        return cell;
    }
}

#warning 创建PickerView
- (void)creatPickerView
{
    self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144)];
    _pickView.backgroundColor = [UIColor huiseColor];
    _pickView.delegate = self;
    _pickView.dataSource = self;
}
#warning 创建PickerView
- (void)creatPickerView:(UIButton *)btn
{
    self.yearArr = [NSMutableArray arrayWithObjects:@"范琦",@"王姜博",@"尹宁", nil];
    self.dayArr = [NSMutableArray arrayWithObjects:@"12345643567",@"13589854452",@"12398989762", nil];
    self.monthArr = [NSMutableArray arrayWithObjects:@"32432@qq.com",@"21064445544@qq.com",@"dskduier@qq.com", nil];
    self.monthStr = @"21064445544@qq.com";
    self.datStr = @"13589854452";
    
    
    if (_pickView2 == nil) {
        
        self.pickView2 = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144)];
        _pickView2.frame = CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144);
        _pickView2.delegate = self;
        _pickView2.dataSource = self;
        [self.view addSubview:_pickView2];
        _pickView2.backgroundColor = [UIColor huiseColor];
        
    }else{
        [self.view addSubview:_pickView2];
    }
    if (_pickTopView == nil) {
        
        self.pickTopView = [[UIView alloc]initWithFrame:CGRectMake(0, _pickView2.frame.origin.y - 30, KSCREENWIDTH, 30)];
        [self.view addSubview:_pickTopView];
        [self.view bringSubviewToFront:_pickTopView];
        _pickTopView.backgroundColor = HUIBACKGROUNDCOLOR;

    }else{
        [self.view addSubview:_pickTopView];

    }
    
    
    UIButton *done = [UIButton buttonWithType:UIButtonTypeSystem];
    [done setTitle:@"Done" forState:UIControlStateNormal];
    done.frame = CGRectMake(KSCREENWIDTH - 60, 0, 40, 30);
    done.tintColor = [UIColor blackColor];
    [_pickTopView addSubview:done];
    [[done rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_pickView2 removeFromSuperview];
        [_pickTopView removeFromSuperview];
        
        
        ExchangeTwoCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        cell.text.text = _str;
        ExchangeTwoCell *cell1 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        cell1.text.text = _datStr;
        ExchangeTwoCell *cell2 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        cell2.text.text = _monthStr;
    }];
    
}

#warning PickerView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == _pickView) {
        return _listArr[row];
    }
    return _yearArr[row];
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == _pickView) {
        self.textFiled.text = _listArr[row];
    }
    else{
        
        _str = _yearArr[row];
        _monthStr = _monthArr[row];
        _datStr = _dayArr[row];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
